package com.lzh.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author liuzhihao
 * @create 2022-07-02 20:21
 *
 * socket 服务端
 */
public class ServerSocketTest {

    public static void main(String[] args) {
        try {
            // 初始化服务端socket并且绑定9999端口
            ServerSocket serverSocket = new ServerSocket(9999);
            // 等待客户端的连接
            Socket waiteConnectSocket = serverSocket.accept();
            // 获取输入流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(waiteConnectSocket.getInputStream()));
            // 读取每一行数据
            String str;
            // 通过while循环不断读取信息
            while ((str = bufferedReader.readLine()) != null){
                // 输出打印
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
